#https://cmake.org/cmake/help/latest/

cmake_minimum_required(VERSION 3.8)
project(openjdk7)
#支持的语言
enable_language(C CXX ASM)

#全局编译选项,比较重要的有2个(-g:在编译的时候，产生调试信息; -O0:让gcc编译器不要优化我们的代码)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0  -fPIC -fcheck-new -Wno-deprecated-declarations -fpermissive")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0  -fPIC -Wno-deprecated-declarations")
# ASM汇编选项:把汇编文件和C++源代码一起编译
SET(ASM_OPTIONS "-x assembler-with-cpp")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS}")

IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
    MESSAGE(STATUS "current platform: Linux ")
    set(PLATFORM linux)
    set(OS linux)
    set(LINUX 1)
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
    MESSAGE(STATUS "current platform: Mac OS X")
    set(PLATFORM bsd)
    set(OS macosx)
    set(MACOSX 1)
ENDIF ()

#------------------开始:jvm动态库(libjvm.so)编译逻辑---------------


set(MAKE_OUTPUT ${CMAKE_SOURCE_DIR}/build/${OS}-amd64-debug)
set(HOTSPOT_GENERATED_DIR ${MAKE_OUTPUT}/hotspot/outputdir/${PLATFORM}_amd64_compiler2/generated)

file(GLOB_RECURSE HOTSPOT_SHARE_SOURCES hotspot/src/share/vm/*.cpp)
file(GLOB_RECURSE HOTSPOT_CPU_SOURCES hotspot/src/cpu/x86/vm/*.cpp)
file(GLOB_RECURSE HOTSPOT_OS_SOURCES hotspot/src/os/${PLATFORM}/vm/*.cpp)
file(GLOB_RECURSE HOTSPOT_POSIX_SOURCES hotspot/src/os/posix/vm/*.cpp)
file(GLOB_RECURSE HOTSPOT_OS_CPU_SOURCES hotspot/src/os_cpu/${PLATFORM}_x86/vm/*.cpp)
file(GLOB_RECURSE MAKE_BUILD_SOURCES ${HOTSPOT_GENERATED_DIR}/*.cpp)

# 将jvmtiEnvRecommended.cpp文件排除掉...
file(GLOB jvmtiEnvRecommended ${HOTSPOT_GENERATED_DIR}/jvmtifiles/jvmtiEnvRecommended.cpp)
# message("jvmtiEnvRecommended: ${jvmtiEnvRecommended}")
list(REMOVE_ITEM MAKE_BUILD_SOURCES ${jvmtiEnvRecommended})

# 将jvmtiEnvStub.cpp文件排除掉...
file(GLOB jvmtiEnvStub ${HOTSPOT_GENERATED_DIR}/jvmtifiles/jvmtiEnvStub.cpp)
# message("jvmtiEnvStub: ${jvmtiEnvStub}")
list(REMOVE_ITEM MAKE_BUILD_SOURCES ${jvmtiEnvStub})

# 将hotspot/src/cpu/x86目录下以_32结尾的cpp文件排除掉...
file(GLOB REMOVE_SOURCES hotspot/src/cpu/x86/vm/*_32.cpp)
# message("REMOVE_SOURCES: ${REMOVE_SOURCES}")
list(REMOVE_ITEM HOTSPOT_CPU_SOURCES ${REMOVE_SOURCES})

# 将hotspot/src/share/vm/adlc目录下的cpp文件排除掉...
file(GLOB REMOVE_ADLC_SOURCES hotspot/src/share/vm/adlc/*.cpp)
# message("REMOVE_ADLC_SOURCES: ${REMOVE_ADLC_SOURCES}")
list(REMOVE_ITEM HOTSPOT_SHARE_SOURCES ${REMOVE_ADLC_SOURCES})

# 将hotspot/src/share/vm/shark目录下的cpp文件排除掉...
file(GLOB REMOVE_SHARK_SOURCES hotspot/src/share/vm/shark/*.cpp)
# message("REMOVE_SHARK_SOURCES: ${REMOVE_SHARK_SOURCES}")
list(REMOVE_ITEM HOTSPOT_SHARE_SOURCES ${REMOVE_SHARK_SOURCES})

file(GLOB CPP_SOURCES
        ${HOTSPOT_SHARE_SOURCES}
        ${HOTSPOT_CPU_SOURCES}
        ${HOTSPOT_OS_SOURCES}
        ${HOTSPOT_POSIX_SOURCES}
        ${HOTSPOT_OS_CPU_SOURCES}
        ${MAKE_BUILD_SOURCES})

#放到了build后的bin目录下
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gamma_build/${OS})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gamma_build/${OS})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/gamma_build/${OS})

# 添加jvm的动态链接库...
add_library(jvm SHARED ${CPP_SOURCES} hotspot/src/os_cpu/${PLATFORM}_x86/vm/${PLATFORM}_x86_64.s)

target_compile_features(jvm PRIVATE c_std_99 cxx_std_98)
target_include_directories(jvm PRIVATE hotspot/src/share/vm)
target_include_directories(jvm PRIVATE ${HOTSPOT_GENERATED_DIR})
target_include_directories(jvm PRIVATE hotspot/src/share/vm/precompiled)
target_include_directories(jvm PRIVATE hotspot/src/share/vm/prims)
target_include_directories(jvm PRIVATE hotspot/src/cpu/x86/vm)
target_include_directories(jvm PRIVATE hotspot/src/os/${PLATFORM}/vm)
target_include_directories(jvm PRIVATE hotspot/src/os/posix/vm)
target_include_directories(jvm PRIVATE hotspot/src/os_cpu/${PLATFORM}_x86/vm)

# 设置编译时预定义的宏
target_compile_definitions(jvm PUBLIC -DAMD64
        -D_LP64
        -DCOMPILER1
        -DCOMPILER2
        #在build时包含所有GC的实现
        -DINCLUDE_ALL_GCS
        -DASSERT    # 是否开启断言代码
        -DINCLUDE_JVMTI  #jvmti
        -DVM_LITTLE_ENDIAN
        -D_GNU_SOURCE
        -DLINUX
        -DINCLUDE_JVMTI=1
        -DTARGET_ARCH_x86
        -DTARGET_OS_FAMILY_${PLATFORM}
        -DTARGET_ARCH_MODEL_x86_64
        -DTARGET_OS_ARCH_${PLATFORM}_x86
        -DTARGET_OS_ARCH_MODEL_${PLATFORM}_x86_64
        -DTARGET_COMPILER_gcc
        -DHOTSPOT_RELEASE_VERSION="24.95-b00"
        -DHOTSPOT_BUILD_TARGET="debug"
        -DHOTSPOT_BUILD_USER="dengyouxu"
        -DHOTSPOT_LIB_ARCH="amd64" #架构
        -DHOTSPOT_VM_DISTRO="OpenJDK"
        -DJRE_RELEASE_VERSION="1.7.0-internal-debug-dengyouxu_2025_10_08_01_44-b00")

#需要链接的库
target_link_libraries(jvm dl pthread)

#------------------开始:java动态库(libjava.so)编译逻辑---------------

file(GLOB_RECURSE JDK_SHARE_C_SOURCES jdk/src/share/*.c)
file(GLOB_RECURSE JDK_OS_C_SOURCES jdk/src/${PLATFORM}/*.c)

#------------------------开始编译我们的debug入口类:gamma-------------------------------

add_executable(gamma
        gamma_launcher/java.c
        gamma_launcher/jli_util.c
        gamma_launcher/wildcard.c
        gamma_launcher/java_md.c)

# 设置包含路径
target_include_directories(gamma PRIVATE gamma_launcher)
target_include_directories(gamma PRIVATE hotspot/src/share/vm)
target_include_directories(gamma PRIVATE hotspot/src/share/vm/prims)
target_include_directories(gamma PRIVATE hotspot/src/cpu/x86/vm)
target_include_directories(gamma PRIVATE hotspot/src/os/${PLATFORM}/vm)
# 设置链接的库
target_link_libraries(gamma jvm pthread dl)
# 设置编译时预定义的宏
target_compile_definitions(gamma PUBLIC
        -DGAMMA
        -DLINK_INTO_LIBJVM
        -DARCH="amd64"
        -DFULL_VERSION="24.95-b00"
        -DLAUNCHER_TYPE="gamma")